package com.edu.study.base.rest;

import com.edu.study.base.stuCourseExercisesDetail.domain.StuCourseExercisesDetail;
import com.edu.study.base.stuCourseExercisesDetail.service.IStuCourseExercisesDetailService;
import com.edu.study.base.stuCourseExercisesResult.domain.StuCourseExercisesResult;
import com.edu.study.base.stuCourseExercisesResult.service.IStuCourseExercisesResultService;
import com.edu.study.base.stuCourseSutdent.domain.StuCourseSutdent;
import com.edu.study.base.stuCourseSutdent.service.IStuCourseSutdentService;
import com.edu.study.base.stuCourseSutdentDetail.domain.StuCourseSutdentDetail;
import com.edu.study.base.stuCourseSutdentDetail.service.IStuCourseSutdentDetailService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author zuowei
 * @version 1.0
 * @date 2021-06-30 16:18
 */
@Api(tags = "6、课程学习状态相关接口")
@RestController
@RequestMapping("/api/courseStatus")
public class AppCourseStudentController extends BaseController {

    @Autowired
    private IStuCourseSutdentService stuCourseSutdentService;
    @Autowired
    private IStuCourseSutdentDetailService stuCourseSutdentDetailService;
    @Autowired
    private IStuCourseExercisesResultService stuCourseExercisesResultService;
    @Autowired
    private IStuCourseExercisesDetailService stuCourseExercisesDetailService;


    @ApiOperation("更新课程及课程模块学习状态")
    @PostMapping("/updateCourseCatalogStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员id", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", required = true),
            @ApiImplicitParam(name = "courseSubCatalogId", value = "课程内容模块目录id(如：生词学习、课程学习，课程精讲)", required = true),
            @ApiImplicitParam(name = "status", value = "学习状态 (0:未开始，1：学习中，2 已学完)", required = true)

    })
    public AjaxResult updateCourseCatalogStatus(String memberId, String courseId, String courseSubCatalogId, String status) {
        int result = 0;
        StuCourseSutdentDetail courseSutdentDetail = new StuCourseSutdentDetail();
        courseSutdentDetail.setMemberId(memberId);
        courseSutdentDetail.setCourseId(courseId);
        courseSutdentDetail.setSubcatalogId(courseSubCatalogId);
        List<StuCourseSutdentDetail> stuCourseSutdentDetails = stuCourseSutdentDetailService.selectStuCourseSutdentDetailList(courseSutdentDetail);
        courseSutdentDetail.setCourseLearnStatus(status);
        if (stuCourseSutdentDetails != null && stuCourseSutdentDetails.size() > 0) {
            //状态是2说明已学完，设置学完时间
            if ("2".equals(courseSutdentDetail.getCourseLearnStatus())) {
                courseSutdentDetail.setEndLearnDate(new Date());
            }

            // member_id + course_id + subcatalog_id 在表内唯一
            // 若存在可放心使用
            courseSutdentDetail.setId(stuCourseSutdentDetails.get(0).getId());

            result = stuCourseSutdentDetailService.updateStuCourseSutdentDetail(courseSutdentDetail);
        } else {
            courseSutdentDetail.setStartLearnDate(new Date());//开始学习时间
            result = stuCourseSutdentDetailService.insertStuCourseSutdentDetail(courseSutdentDetail);
        }

        int result2 = updateCourseStatus(memberId, courseId);
        return toAjax(result * result2);
    }

    /**
     * 更新课程状态
     */
    public int updateCourseStatus(String memberId, String courseId) {
        int result = 0;
        StuCourseSutdent stuCourseSutdent = new StuCourseSutdent();
        stuCourseSutdent.setMemberId(memberId);
        stuCourseSutdent.setCourseId(courseId);
        List<StuCourseSutdent> list = stuCourseSutdentService.selectStuCourseSutdentList(stuCourseSutdent);
        if (list != null && list.size() > 0) {
            stuCourseSutdent.setId(list.get(0).getId());
            StuCourseSutdentDetail courseSutdentDetail = new StuCourseSutdentDetail();
            courseSutdentDetail.setMemberId(memberId);
            courseSutdentDetail.setCourseId(courseId);
            List<StuCourseSutdentDetail> stuCourseSutdentDetails = stuCourseSutdentDetailService.selectStuCourseSutdentDetailList(courseSutdentDetail);
            //课程下的内容全部学完后，课程才算学完
            if (stuCourseSutdentDetails != null && stuCourseSutdentDetails.size() > 0) {
                for (StuCourseSutdentDetail detail : stuCourseSutdentDetails) {
                    if (!"2".equals(detail.getCourseLearnStatus())) {
                        stuCourseSutdent.setCourseLearnStatus("1");
                        break;
                    } else {
                        stuCourseSutdent.setCourseLearnStatus("2");
                    }
                }
                result = stuCourseSutdentService.updateStuCourseSutdent(stuCourseSutdent);
            }

        } else {
            stuCourseSutdent.setCourseLearnStatus("1");
            result = stuCourseSutdentService.insertStuCourseSutdent(stuCourseSutdent);
        }

        return result;
    }


    @ApiOperation("更新课后练习状态")
    @PostMapping("/updateCourseExerciseStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员id", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", required = true),
            @ApiImplicitParam(name = "courseExerciseCatalogId", value = "课程练习目录id", required = true),
            @ApiImplicitParam(name = "courseExerciseSubCatalogId", value = "课程练习子目录id", required = true),
            @ApiImplicitParam(name = "status", value = "学习状态 (0:未开始，1：学习中，2 已学完)", required = true)

    })
    public AjaxResult updateCourseExerciseStatus(String memberId, String courseId, String courseExerciseCatalogId, String courseExerciseSubCatalogId, String status) {
        int result = 0;
        StuCourseExercisesDetail exercisesDetail = new StuCourseExercisesDetail();
        exercisesDetail.setMemberId(memberId);
        exercisesDetail.setCourseId(courseId);
        exercisesDetail.setProductItemId(courseExerciseCatalogId);//课程练习目录id
        exercisesDetail.setSubcatalogId(courseExerciseSubCatalogId);//课程练习子目录id
        List<StuCourseExercisesDetail> courseExercisesDetailList = stuCourseExercisesDetailService.selectStuCourseExercisesDetailList(exercisesDetail);
        exercisesDetail.setCourseLearnStatus(status);
        if (courseExercisesDetailList != null && courseExercisesDetailList.size() > 0) {
            exercisesDetail.setId(courseExercisesDetailList.get(0).getId());
            if ("2".equals(exercisesDetail.getCourseLearnStatus())) {
                exercisesDetail.setEndLearnDate(new Date());
            }
            result = stuCourseExercisesDetailService.updateStuCourseExercisesDetail(exercisesDetail);
        } else {
            exercisesDetail.setStartLearnDate(new Date());
            result = stuCourseExercisesDetailService.insertStuCourseExercisesDetail(exercisesDetail);
        }
        int result2 = 1;
        if (!courseExerciseCatalogId.equals(courseExerciseSubCatalogId))
            result2 = updataCourseExercisesStatus(memberId, courseId, courseExerciseCatalogId, status);

        return toAjax(result * result2);
    }

    /**
     * 更新课后练习状态
     *
     * @param memberId           会员id
     * @param courseId           课程id
     * @param exercisesCatalogId 课后练习目录id
     * @param status             学习状态
     * @return
     */
    public int updataCourseExercisesStatus(String memberId, String courseId, String exercisesCatalogId, String status) {
        int result = 0;
        StuCourseSutdentDetail courseSutdentDetail = new StuCourseSutdentDetail();
        courseSutdentDetail.setMemberId(memberId);
        courseSutdentDetail.setCourseId(courseId);
        courseSutdentDetail.setSubcatalogId(exercisesCatalogId);
        List<StuCourseSutdentDetail> stuCourseSutdentDetails = stuCourseSutdentDetailService.selectStuCourseSutdentDetailList(courseSutdentDetail);
        //课后练习的内容全部完成后，课程练习才算完成
        if (stuCourseSutdentDetails != null && stuCourseSutdentDetails.size() > 0) {
            courseSutdentDetail.setId(stuCourseSutdentDetails.get(0).getId());
            StuCourseExercisesDetail exercisesDetail = new StuCourseExercisesDetail();
            exercisesDetail.setMemberId(memberId);
            exercisesDetail.setCourseId(courseId);
            exercisesDetail.setProductItemId(exercisesCatalogId);//课程练习目录id
            List<StuCourseExercisesDetail> courseExercisesDetailList = stuCourseExercisesDetailService.selectStuCourseExercisesDetailList(exercisesDetail);
            if (courseExercisesDetailList != null && courseExercisesDetailList.size() > 0) {
                for (StuCourseExercisesDetail exercisesDetail1 : courseExercisesDetailList) {
                    if (!"2".equals(exercisesDetail1.getCourseLearnStatus())) {
                        courseSutdentDetail.setCourseLearnStatus("1");
                        break;
                    } else {
                        courseSutdentDetail.setCourseLearnStatus("2");
                    }
                }
                result = stuCourseSutdentDetailService.updateStuCourseSutdentDetail(courseSutdentDetail);
            }

        } else {
            result = stuCourseSutdentDetailService.insertStuCourseSutdentDetail(courseSutdentDetail);
        }
        int courseStatus = updateCourseStatus(memberId, courseId);
        result = courseStatus * result;
        return result;
    }


    @ApiOperation(value = "上传录音文件", hidden = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员id", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", required = true),
            @ApiImplicitParam(name = "questionId", value = "试题Id", required = true),
            @ApiImplicitParam(name = "file", value = "录音文件", required = true)

    })
    @PostMapping(value = "/uploadAudioFile", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public AjaxResult uploadFile(HttpServletRequest request, MultipartFile file, String memberId, String courseId, String questionId) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath() + "/audio";
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String url = ServletUtils.getDomainUrl() + fileName;
        StuCourseExercisesResult courseExercisesResult = new StuCourseExercisesResult();
        courseExercisesResult.setMemberId(memberId);
        courseExercisesResult.setCourseId(courseId);
        courseExercisesResult.setQuestionId(questionId);
        courseExercisesResult.setAudioFileUrl(url);
        stuCourseExercisesResultService.insertStuCourseExercisesResult(courseExercisesResult);
        return AjaxResult.success("上传成功");
    }


}
